Strcatroots = {'A','B','C','E','J','S'};
FileNames = {'output\ComprehensiveStudy_6AAll','output\ComprehensiveStudy_6BAll','output\ComprehensiveStudy_6CAll','output\ComprehensiveStudy_6EAll','output\ComprehensiveStudy_6JAll','output\ComprehensiveStudy_6SAll'};
filecounter = 0;
while(filecounter<5)
    
    filecounter = filecounter + 1;
    load(FileNames{1,filecounter},'trimmedData');
    hours = hour(trimmedData(:,1));
    minutes = minute(trimmedData(:,1));
    seconds = second(trimmedData(:,1));
    hourChange = (abs(hours(2:end)-hours(1:end-1))>0);
    minutesChange = ((((minutes(2:end)>=15) + (minutes(1:end-1)<15))>1.5)+(((minutes(2:end)>=30) + (minutes(1:end-1)<30))>1.5)+(((minutes(2:end)>=45) + (minutes(1:end-1)<45))>1.5)+(((minutes(2:end)<=15) + (minutes(1:end-1)>45))>1.5))>0;
    minutesChange2 = abs(minutes(2:end)-minutes(1:end-1))>0.001;
    secondsChange2 = abs(seconds(2:end)-seconds(1:end-1))>0.001;
    
    changes15 = ((hourChange>0)+(minutesChange>0))>0.5;
    changes15 = [1;changes15];
    indices15 = changes15.*(1:size(changes15,1))';
    indices15 = indices15(indices15>0);
    numIntervals = size(indices15,1)-1;
    intervalValues15 = zeros(numIntervals,3);
    counter = 0;
    while(counter<numIntervals)
        counter = counter + 1;
        intervalValues15(counter,:) = [sum(trimmedData(indices15(counter,1):indices15(counter+1,1),2)) sum(abs(trimmedData(indices15(counter,1):indices15(counter+1,1),2))) sum(trimmedData(indices15(counter,1):indices15(counter+1,1),3)) ];
    end
    [X15,Y15,BetaHat15,YHat15,ehat15,Sandwich15] = OLSGenericProcessor(abs(intervalValues15(:,1)).^0.5,intervalValues15(:,3).*sign(intervalValues15(:,1)));
    z15 = (BetaHat15./diag(sqrt(Sandwich15)))*sqrt(size(X15,1));
    r2_15 = 1- sum(ehat15.^2)/sum((Y15-mean(Y15)).^2);
            
    changes60 = ((hourChange>0))>0.5;
    changes60 = [1;changes60];
    indices60 = changes60.*(1:size(changes60,1))';
    indices60 = indices60(indices60>0);
    numIntervals = size(indices60,1)-1;
    intervalValues60 = zeros(numIntervals,3);
    counter = 0;
    while(counter<numIntervals)
        counter = counter + 1;
        intervalValues60(counter,:) = [sum(trimmedData(indices60(counter,1):indices60(counter+1,1),2)) sum(abs(trimmedData(indices60(counter,1):indices60(counter+1,1),2))) sum(trimmedData(indices60(counter,1):indices60(counter+1,1),3)) ];
    end
    [X60,Y60,BetaHat60,YHat60,ehat60,Sandwich60] = OLSGenericProcessor(abs(intervalValues60(:,1)).^0.5,intervalValues60(:,3).*sign(intervalValues60(:,1)));
    z60 = (BetaHat60./diag(sqrt(Sandwich60)))*sqrt(size(X60,1));
    r2_60 = 1- sum(ehat60.^2)/sum((Y60-mean(Y60)).^2);
    
    changes1 = minutesChange2;
    changes1 = [1;changes1];
    indices1 = changes1.*(1:size(changes1,1))';
    indices1 = indices1(indices1>0);
    numIntervals = size(indices1,1)-1;
    intervalValues1 = zeros(numIntervals,3);
    counter = 0;
    while(counter<numIntervals)
        counter = counter + 1;
        intervalValues1(counter,:) = [sum(trimmedData(indices1(counter,1):indices1(counter+1,1),2)) sum(abs(trimmedData(indices1(counter,1):indices1(counter+1,1),2))) sum(trimmedData(indices1(counter,1):indices1(counter+1,1),3)) ];
    end
    [X1,Y1,BetaHat1,YHat1,ehat1,Sandwich1] = OLSGenericProcessor(abs(intervalValues1(:,1)).^0.5,intervalValues1(:,3).*sign(intervalValues1(:,1)));
    z1 = (BetaHat1./diag(sqrt(Sandwich1)))*sqrt(size(X1,1));
    r2_1 = 1- sum(ehat1.^2)/sum((Y1-mean(Y1)).^2);
    
    changes1s = secondsChange2;
    changes1s = [1;changes1s];
    indices1s = changes1s.*(1:size(changes1s,1))';
    indices1s = indices1s(indices1s>0);
    numIntervalss = size(indices1s,1)-1;
    intervalValues1s = zeros(numIntervalss,3);
    counter = 0;
    while(counter<numIntervalss)
        counter = counter + 1;
        intervalValues1s(counter,:) = [sum(trimmedData(indices1s(counter,1):indices1s(counter+1,1),2)) sum(abs(trimmedData(indices1s(counter,1):indices1s(counter+1,1),2))) sum(trimmedData(indices1s(counter,1):indices1s(counter+1,1),3)) ];
    end
    [X1s,Y1s,BetaHat1s,YHat1s,ehat1s,Sandwich1s] = OLSGenericProcessor(abs(intervalValues1s(:,1)).^0.5,intervalValues1s(:,3).*sign(intervalValues1s(:,1)));
    z1s = (BetaHat1s./diag(sqrt(Sandwich1s)))*sqrt(size(X1s,1));
    r2_1s = 1- sum(ehat1s.^2)/sum((Y1s-mean(Y1s)).^2);
    
    
    str = strcat('output\','RobustIntervalPQ6',Strcatroots{1,filecounter});
    save(str,'BetaHat15','BetaHat1','BetaHat1s','BetaHat60','Sandwich15','Sandwich1','Sandwich1s','Sandwich60','z15','z60','r2_15','r2_60','z1','z1s','r2_1','r2_1s');
    
    
end